//
//  XFYDrawerView.m
//  SDKSample
//
//  Created by 小肥羊 on 15/9/23.
//  Copyright © 2015年 Zhu‘s Game House. All rights reserved.
//

#import "XFYDrawerView.h"

@implementation XFYBaseDrawerView

#pragma mark - 触摸调用方法
/*! @brief 触摸开始时调用此方法 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸开始时调用此方法");
    //保存触摸起始点位置
    CGPoint point = [[touches anyObject] locationInView:self];
    self.startPoint = point;
    
    //该view置于最前
    [[self superview] bringSubviewToFront:self];
}
/*! @brief 手离开屏幕，触摸事件结束 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"手离开屏幕，触摸事件结束");
}
/*! @brief 手在界面上移动 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    NSLog(@"手在界面上移动");
    //计算位移=当前位置-起始位置
    CGPoint point = [[touches anyObject] locationInView:self];
    float dx;
    float dy;
    
    switch (self.drawerViewType) {
        case XFYDrawerViewAnyDirectionType:
            dx = point.x - self.startPoint.x;
            dy = point.y - self.startPoint.y;
            break;
        case XFYDrawerViewHorizontalDirectionType:
            dx = point.x - self.startPoint.x;
            dy = 0;
            break;
        case XFYDrawerViewVerticalDirectionType:
            dx = 0;
            dy = point.y - self.startPoint.y;
            break;
        default:
            dx = point.x - self.startPoint.x;
            dy = point.y - self.startPoint.y;
            break;
    }
    
    //计算移动后的view中心点
    CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
    
    
    /* 限制用户不可将视图托出屏幕 */
    float halfx = CGRectGetMidX(self.bounds);
    //x坐标左边界
    newcenter.x = MAX(halfx, newcenter.x);
    //x坐标右边界
    newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x);
    
    //y坐标同理
    float halfy = CGRectGetMidY(self.bounds);
    newcenter.y = MAX(halfy, newcenter.y);
    newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y);
    
    //移动view
    self.center = newcenter;
}
/*! @brief 手离开界面 */
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"手离开界面");
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end





@implementation XFYDrawerView
#pragma mark - 初始化
- (id)initWithHeaderView:(UIView *)headerView TailView:(UIView *)tailView
{
    self = [super initWithFrame:(CGRect){headerView.frame.origin,CGSizeMake(headerView.bounds.size.width + tailView.bounds.size.width, headerView.bounds.size.height)}];
    if (self) {
        self.headerView = headerView;
        self.tailView = tailView;
        [self.headerView setFrame:(CGRect){CGPointZero,self.headerView.bounds.size}];
        [self addSubview:self.headerView];
        [self.tailView setFrame:CGRectMake(self.headerView.bounds.size.width, 0, self.tailView.bounds.size.width, self.headerView.bounds.size.height)];
        [self addSubview:self.tailView];
    }
    return self;
}

#pragma mark - 公用方法
- (void)openView:(BOOL)isOpen
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:@"openDrawer" context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];
    CGRect rect = self.frame;
    if (isOpen) {
        rect.origin.x = self.superview.bounds.size.width - self.bounds.size.width;
    } else {
        rect.origin.x = self.superview.bounds.size.width - self.headerView.bounds.size.width;
    }
    [self setFrame:rect];
    [UIView commitAnimations];
}

#pragma mark - 触摸调用方法
/*! @brief 手在界面上移动 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //计算位移=当前位置-起始位置
    CGPoint point = [[touches anyObject] locationInView:self];
    float dx = point.x - self.startPoint.x;
    float dy = 0;
    //计算移动后的view中心点
    CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy);
    
    //x坐标左边界
    newcenter.x = MAX(self.superview.bounds.size.width - (self.tailView.bounds.size.width + self.headerView.bounds.size.width)/2, newcenter.x);
    //x坐标右边界
    newcenter.x = MIN(self.superview.bounds.size.width + (self.tailView.bounds.size.width - self.headerView.bounds.size.width)/2, newcenter.x);
    
    //移动view
    self.center = newcenter;
}

/*! @brief 手离开界面 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    float centerX = self.superview.bounds.size.width - self.frame.size.width/2;
    NSLog(@"%f,%f",self.frame.origin.x,centerX);
    if (self.frame.origin.x < centerX) {
        [self openView:YES];
    } else {
        [self openView:NO];
    }
}
@end